package com.xiaoxu.intranetweb.mybatisplusSzappdb.controller.plants;

import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson.JSONObject;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.business.plants.PlantsBu;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.dao.UserInfoDao;
import com.xiaoxu.intranetweb.util.PermissionCheck;
import com.xiaoxu.intranetweb.util.SzResult;
import com.xiaoxu.intranetweb.util.WebFunctionUtil;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/plants")
public class plantsController {
    @Autowired
    private PlantsBu plantsBu;
    @Autowired
    private UserInfoDao userInfoDao;
    @Autowired
    private WebFunctionUtil webFunctionUtil;
    @PostMapping("/getPlants")
    public SzResult getPlants(@RequestBody JSONObject jsonObject, HttpServletRequest request) {
        String value = jsonObject.getString("value");
        return plantsBu.getPlants(value);
    }
    @PostMapping("/addPlants")
    @PermissionCheck("addPlants")
    public SzResult addPlants(@RequestBody JSONObject jsonObject, HttpServletRequest request) throws Exception {
        String plantFullName = jsonObject.getString("plantFullName");
        String plantShortName = jsonObject.getString("plantShortName");
        String token = request.getHeader("Authorization");      String plantId = request.getHeader("Plant-Id");
        return plantsBu.addPlants(plantFullName, plantShortName,token,plantId);
    }
    @PostMapping("/updatePlants")
    @PermissionCheck("updatePlants")
    public SzResult updatePlants(@RequestBody JSONObject jsonObject, HttpServletRequest request) throws Exception {
        String plantFullName = jsonObject.getString("plantFullName");
        String plantShortName = jsonObject.getString("plantShortName");
        String plantId = request.getHeader("Plant-Id");
        String token = request.getHeader("Authorization");
        return plantsBu.updatePlants(plantFullName, plantShortName, plantId,token);
    }

    @GetMapping("/deletePlants")
    @PermissionCheck("deletePlants")
    public SzResult deletePlants(@RequestParam(value = "plantId") String plantId, HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        String plantId1 = request.getHeader("Plant-Id");
        return plantsBu.deletePlants(plantId,token,plantId1);
    }
}
